import 'dart:convert' show json;

import 'package:flutter/cupertino.dart';
import 'package:ribo_base_core/base_styles/base_chart_style.dart';

import 'base_chart_formatter.dart';
import 'base_model_asT.dart';

class BaseChartPieModel {
  /// https://echarts.apache.org/zh/option.html#tooltip.formatter
  BaseChartFormatter tooltipFormatter;

  /// https://echarts.apache.org/zh/option.html#series-pie.label.formatter
  BaseChartFormatter labelFormatter;

  /// 图表样式
  BaseChartStyle style;

  /// 是否显示为玫瑰图（南丁格尔图）
  bool isRose;

  /// 标签的位置 outside、inner、center
  String labelPosition;

  /// 半径
  String radius;

  /// 图表数据
  List<BaseChartPieData> data;

  BaseChartPieModel({
    this.tooltipFormatter,
    this.labelFormatter,
    this.isRose = false,
    this.labelPosition = 'inner',
    this.radius,
    this.style,
    @required this.data,
  });

  factory BaseChartPieModel.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }

    final List<BaseChartPieData> data =
        jsonRes['data'] is List ? <BaseChartPieData>[] : null;
    if (data != null) {
      for (final dynamic item in jsonRes['data']) {
        if (item != null) {
          data.add(BaseChartPieData.fromJson(asT<Map<String, dynamic>>(item)));
        }
      }
    }
    return BaseChartPieModel(
      tooltipFormatter: asT<BaseChartFormatter>(jsonRes['tooltipFormatter']),
      labelFormatter: asT<BaseChartFormatter>(jsonRes['labelFormatter']),
      style: asT<BaseChartStyle>(jsonRes['style']),
      isRose: asT<bool>(jsonRes['isRose']),
      labelPosition: asT<String>(jsonRes['labelPosition']),
      radius: asT<String>(jsonRes['radius']),
      data: data,
    );
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'tooltipFormatter': tooltipFormatter,
        'labelFormatter': labelFormatter,
        'style': style,
        'isRose': isRose,
        'labelPosition': labelPosition,
        'radius': radius,
        'data': data,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class BaseChartPieData {
  BaseChartPieData({String name, this.value, this.itemStyle, this.isDate = true})
      : this.name = isDate ? name.substring(name.lastIndexOf('-')+1) : name; //只显示最后的日期

  factory BaseChartPieData.fromJson(Map<String, dynamic> jsonRes) =>
      jsonRes == null
          ? null
          : BaseChartPieData(
              name: asT<String>(jsonRes['name']),
              value: asT<double>(jsonRes['value']),
              itemStyle: asT<Map<String, dynamic>>(jsonRes['itemStyle']),
            );

  /// 数据名称
  String name;

  /// 数据值
  double value;

  /// 是否为日期 默认是
  bool isDate;

  /// https://echarts.apache.org/zh/option.html#series-bar.itemStyle
  Map<String, dynamic> itemStyle;

  Map<String, dynamic> toJson() {
    var json = <String, dynamic>{
      'name': name,
      'value': value,
      'itemStyle': itemStyle
    };
    json.removeWhere((key, value) => value == null);
    return json;
  }

  @override
  String toString() {
    return json.encode(this);
  }
}
